`
https://leetcode.cn/problems/binary-search-tree-iterator/
`

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 */
var BSTIterator = function (root) {
  // https://labuladong.online/algo/images/iter-binary-tree/1.gif
  // 模拟递归栈
  this.stk = [];

  // 左侧树枝一撸到底
  this.pushLeftBranch = function (p) {
    while (p != null) {
      this.stk.push(p);
      p = p.left;
    }
  }

  this.pushLeftBranch(root);
};

/**
* @return {number}
*/
BSTIterator.prototype.next = function () {
  const p = this.stk.pop();
  this.pushLeftBranch(p.right);
  return p.val;
};

/**
* @return {boolean}
*/
BSTIterator.prototype.hasNext = function () {
  return this.stk.length > 0;
};

/** 
* Your BSTIterator object will be instantiated and called as such:
* var obj = new BSTIterator(root)
* var param_1 = obj.next()
* var param_2 = obj.hasNext()
*/